package com.neusoft.hifly.rpc.attachment.service.impl;

import java.util.List;

import com.neusoft.hifly.commons.lang.StringConstants;
import com.neusoft.hifly.core.log.annotation.LogType;
import com.neusoft.hifly.core.log.annotation.OperationLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.hifly.commons.lang.LongUtil;
import com.neusoft.hifly.core.log.Log;
import com.neusoft.hifly.core.pojo.ApiCode;
import com.neusoft.hifly.core.pojo.ApiResponse;
import com.neusoft.hifly.rpc.attachment.dao.IFileDao;
import com.neusoft.hifly.rpc.attachment.domain.FileDTO;
import com.neusoft.hifly.rpc.attachment.service.IFileService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * Title: 附件服务
 * <p>
 * Description: 附件接口
 * <p>
 * Copyright: Copyright (c) 2008
 * <p>
 * Company: 东软汉枫
 *
 * @author lihui
 * @version 1.0.0
 */
@Service
public class FileServiceImpl implements IFileService {

	@Autowired(required = false)
	private IFileDao fileDao;

	/**
	 * 查询关联类型和主表ID为条件的记录
	 *
	 * @param objType
	 *            关联类型
	 * @param objLogicId
	 *            关联ID
	 * @param orderBy
	 *            自定义排序
	 * @return 返回附件列表
	 */
	@Override
	public List<FileDTO> findByObjTypeAndObjLogicId(String objType, long objLogicId, String orderBy) {
		if (LongUtil.isEmpty(objLogicId)) {
			return null;
		}
		final ApiResponse<List<FileDTO>> result = fileDao.findByObjLogicId(objType, objLogicId, orderBy);

		if (result.getCode() == ApiCode.ERROR.getCode()) {
			Log.error(result.getMessage());
			return null;
		}

		return result.getData();
	}

	/**
	 * 批量删除对象
	 *
	 * @param organCode
	 *            租户编码
	 * @param organLogicId
	 *            租户逻辑ID
	 * @param logicIds
	 *            对象ID，多id使用英文逗号分隔
	 */
	@Override
	public void deleteByBatch(final String organCode, final long organLogicId, final String logicIds) {
		if (StringUtils.isEmpty(organCode) || StringUtils.isEmpty(logicIds)) {
			return;
		}
		final String[] logicIdArr = logicIds.split(StringConstants.STRING_SPLIT_FLAG_3);
		if (logicIdArr == null) {
			return;
		}
		for (final String logicId : logicIdArr) {
			if (StringUtils.isEmpty(logicId)) {
				continue;
			}
			this.deleteByLogicId(organCode, organLogicId, Long.parseLong(logicId));
		}
	}

	/**
	 * 根据ID删除对象
	 *
	 * @param organCode
	 *            租户编码
	 * @param organLogicId
	 *            租户逻辑ID
	 * @param logicId
	 *            对象ID
	 */
	@Override
	public void deleteByLogicId(final String organCode, final long organLogicId, final long logicId) {
		if (StringUtils.isEmpty(organCode)) {
			return;
		}
		final ApiResponse<?> result =fileDao.delete( logicId+"");
	}
}
